//
//  API_Setting.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import Moya

enum API_Setting {
    /// 退出账号
    case loginOut
    /// 注销账号
    case deleteUser
    /// 充值列表
    case rechargeList
    /// 充值记录
    case rechargeRecordList(Int)
    /// 购买记录
    case buyRecordList(Int)
    /// 金币奖励记录
    case bonusList(Int)
    /// 历史记录
    case viewedList(Int)
    /// 删除历史记录
    case viewedDelete(String)
    /// 收藏列表
    case collectList(Int, Int)
    /// 删除收藏列表
    case collectDelete(String)
    /// 新用户限时充值
    case rechargeLimited
    /// 版本升级
    case versionUpgrade
}

let setting_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Setting>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_setting = MoyaProvider<API_Setting>(requestClosure: setting_timeoutClosure)

extension API_Setting: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .loginOut:
            return "/Shield/logout"
        case .deleteUser:
            return "/Shield/accountCancel"
        case .rechargeList:
            return "/Money/gold"
        case .rechargeRecordList(_):
            return "/Money/history"
        case .buyRecordList(_):
            return "/BookStore/history"
        case .bonusList(_):
            return "/Shield/getUserGoldSource"
        case .viewedList(_):
            return "/User/history"
        case .viewedDelete:
            return "/User/bitchHistory"
        case .collectList(_, _):
            return "/User/favorite"
        case .collectDelete:
            return "/User/bitchFavorite"
        case .rechargeLimited:
            return "/Limitedrecharge/gold"
        case .versionUpgrade:
            return "/Version/check_version"
        }
    }

    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .loginOut:
            break
        case .deleteUser:
            break
        case .rechargeList:
            break
        case .rechargeRecordList(let page):
            ps["page"] = page
            ps["num"] = 10
        case .buyRecordList(let page):
            ps["page"] = page
            ps["num"] = 10
        case .bonusList(let page):
            ps["page"] = page
            ps["num"] = 10
        case .viewedList(let page):
            ps["page"] = page
            ps["num"] = 10
        case .viewedDelete(let bookIds):
            ps["bookId"] = bookIds
        case .collectList(let page, let num):
            ps["page"] = page
            ps["num"] = num
        case .collectDelete(let bookIds):
            ps["bookId"] = bookIds
        case .rechargeLimited:
            break
        case .versionUpgrade:
            break
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}

